
package com.joolun.cloud.weixin.admin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.weixin.admin.config.ma.WxMaConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序用户隐私保护指引
 *
 * @author www.joolun.com
 * @date 2019-03-23 21:26:35
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmaprivacysetting")
@Api(value = "wxmaprivacysetting", tags = "小程序用户隐私保护指引")
public class WxMaPrivacySettingController {

	/**
	* 查询小程序用户隐私保护指引
	* @return
	*/
	@ApiOperation(value = "查询小程序用户隐私保护指引")
	@GetMapping("/get/{appId}")
	@PreAuthorize("@ato.hasAuthority('wxma:privacysetting:index')")
	public R getPrivacySetting(@PathVariable("appId") String appId) throws WxErrorException {
		Map<String, Object> map = new HashMap<>();
		String responseContent = WxMaConfiguration.getMaService(appId).post("https://api.weixin.qq.com/cgi-bin/component/getprivacysetting", map);
		JSONObject jsonObject = JSONUtil.parseObj(responseContent);
		return R.ok(jsonObject);
	}

	/**
	 * 配置小程序用户隐私保护指引
	 * @return
	 */
	@ApiOperation(value = "配置小程序用户隐私保护指引")
	@PostMapping("/set/{appId}")
	@PreAuthorize("@ato.hasAuthority('wxma:privacysetting:index')")
	public R setPrivacySetting(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map) throws WxErrorException {
		String responseContent = WxMaConfiguration.getMaService(appId).post("https://api.weixin.qq.com/cgi-bin/component/setprivacysetting", map);
		JSONObject jsonObject = JSONUtil.parseObj(responseContent);
		return R.ok(jsonObject);
	}

}
